package view.ressources {	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author Anthony
	 */	
	public class GraphicRessourcesManager extends EventDispatcher
	{
		//------------------------------- statics ------------------------------\\
		public static const LOADED 					: String = "GraphicRessourcesManager is loaded";
		public static const RESSOURCESDIRECTORYNAME	: String = "../lib/";
		
		private static var _self : GraphicRessourcesManager = new GraphicRessourcesManager();
				
		//------------------------------ variables -----------------------------\\
		private var _imageURLsToLoad 	: Array /*String*/;
		private var _URLtoImageName		: Object /*String*/;
		
		//------------------------------ accessors -----------------------------\\
		public static function get self() : GraphicRessourcesManager { return _self; }
		
		//-------------------------- public functions --------------------------\\
		public function GraphicRessourcesManager()
		{			
			if (_self)
				throw new Error("GraphicRessourcesManager can only be accessed through GraphicRessourcesManager.self");
			else
			{
				
			}
		}
		
		public function init() : void
		{
			_imageURLsToLoad 	= new Array /*String*/();
			_URLtoImageName		= new Object /*String*/();
			
			_imageURLsToLoad.push(RESSOURCESDIRECTORYNAME + "goToNextGraphicPostBrotherButton.png");
			_URLtoImageName["goToNextGraphicPostBrotherButton.png"] = "goToNextGraphicPostBrotherButton";			
			_imageURLsToLoad.push(RESSOURCESDIRECTORYNAME + "goToNextGraphicPostBrotherButton_OVER.png");
			_URLtoImageName["goToNextGraphicPostBrotherButton_OVER.png"] = "goToNextGraphicPostBrotherButton_OVER";
						
			_imageURLsToLoad.push(RESSOURCESDIRECTORYNAME + "goToPreviousGraphicPostBrotherButton.png");
			_URLtoImageName["goToPreviousGraphicPostBrotherButton.png"] = "goToPreviousGraphicPostBrotherButton";			
			_imageURLsToLoad.push(RESSOURCESDIRECTORYNAME + "goToPreviousGraphicPostBrotherButton_OVER.png");
			_URLtoImageName["goToPreviousGraphicPostBrotherButton_OVER.png"] = "goToPreviousGraphicPostBrotherButton_OVER";
			
			_imageURLsToLoad.push(RESSOURCESDIRECTORYNAME + "graphicPostHeadBackground.png");
			_URLtoImageName["graphicPostHeadBackground.png"] = "graphicPostHeadBackground";
			_imageURLsToLoad.push(RESSOURCESDIRECTORYNAME + "graphicPostCorpsBackground.png");
			_URLtoImageName["graphicPostCorpsBackground.png"] = "graphicPostCorpsBackground";
			_imageURLsToLoad.push(RESSOURCESDIRECTORYNAME + "graphicPostFootBackground.png");
			_URLtoImageName["graphicPostFootBackground.png"] = "graphicPostFootBackground";
			
			_imageURLsToLoad.push(RESSOURCESDIRECTORYNAME + "graphicPostHeadBackground_SELECTED.png");
			_URLtoImageName["graphicPostHeadBackground_SELECTED.png"] = "graphicPostHeadBackground_SELECTED";
			_imageURLsToLoad.push(RESSOURCESDIRECTORYNAME + "graphicPostCorpsBackground_SELECTED.png");
			_URLtoImageName["graphicPostCorpsBackground_SELECTED.png"] = "graphicPostCorpsBackground_SELECTED";
			_imageURLsToLoad.push(RESSOURCESDIRECTORYNAME + "graphicPostFootBackground_SELECTED.png");
			_URLtoImageName["graphicPostFootBackground_SELECTED.png"] = "graphicPostFootBackground_SELECTED";
			
			imageLoaded();
		}
		
		//------------------------- private functions --------------------------\\
		private function imageLoaded(e : Event = null) : void 
		{
			if (e != null)
			{
				var image : Bitmap = Bitmap(e.target.content);					
				var url : String = e.target.url.substr(e.target.url.lastIndexOf("/") + 1);
				GraphicRessources.self[_URLtoImageName[url]] = image.bitmapData;
			}
			
			if (_imageURLsToLoad.length > 0)
			{			
				var imageUrlToLoad : String = _imageURLsToLoad.pop();			
				var loader : Loader 		= new Loader();
				var request : URLRequest 	= new URLRequest(imageUrlToLoad);
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
				loader.load(request);
			}
			else
			{
				dispatchEvent(new Event(GraphicRessourcesManager.LOADED));
			}
		}
		
	}
}